package com.sora.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.sora.commonutils.exceptionhandler.GuliException;
import com.sora.eduservice.entity.EduChapter;
import com.sora.eduservice.entity.EduCourse;
import com.sora.eduservice.entity.EduCourseDescription;
import com.sora.eduservice.entity.EduTeacher;
import com.sora.eduservice.entity.frontvo.CourseFrontVo;
import com.sora.eduservice.entity.frontvo.CourseWebVo;
import com.sora.eduservice.entity.vo.CourseInfoVo;
import com.sora.eduservice.entity.vo.CoursePublishVo;
import com.sora.eduservice.mapper.EduCourseMapper;
import com.sora.eduservice.service.EduChapterService;
import com.sora.eduservice.service.EduCourseDescriptionService;
import com.sora.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sora.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author sora
 * @since 2022-12-28
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduCourseMapper eduCourseMapper;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    /**
     * 添加课程
     * @param courseInfoVo
     * @return
     */
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        int insert = baseMapper.insert(course);
        if (insert < 1) {
            throw new GuliException(20001, "添加课程信息失败");
        }
        String id = course.getId();
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,description);
        description.setId(id);
        eduCourseDescriptionService.save(description);
        return id;
    }

    /**
     * 得到课程信息
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(description,courseInfoVo);
        return courseInfoVo;
    }

    /**
     * 更新课程信息
     * @param courseInfoVo
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if (i == 0) {
            throw new GuliException(20001, "修改课程信息失败");
        }
        BeanUtils.copyProperties(courseInfoVo,description);
        eduCourseDescriptionService.updateById(description);
    }

    /**
     * 发布课程信息
     * @param id
     * @return
     */
    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        CoursePublishVo vo = eduCourseMapper.getPublishCourseInfo(id);
        return vo;
    }

    /**
     * 删除课程
     * @param courseId
     * @return
     */
    @Override
    public boolean removeCourse(String courseId) {
        eduVideoService.removeVideoByCourseId(courseId);
        eduChapterService.removeChapterByCourseId(courseId);
        eduCourseDescriptionService.removeById(courseId);
        int i = eduCourseMapper.deleteById(courseId);
        System.out.println(i);
        if (i < 1) {
            throw new GuliException(20001,"删除失败");
        }
        return true;
    }

    @Cacheable(value = "course",key = "'selectHotCourse'")
    @Override
    public List<EduCourse> selectHotCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> getCourseFrontInfo(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {
        String title = courseFrontVo.getTitle();
        String subjectId = courseFrontVo.getSubjectId();
        String subjectParentId = courseFrontVo.getSubjectParentId();
        String gmtCreateSort = courseFrontVo.getGmtCreateSort();
        String buyCountSort = courseFrontVo.getBuyCountSort();
        String priceSort = courseFrontVo.getPriceSort();
        String teacherId = courseFrontVo.getTeacherId();
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接条件
        //一级分类
        if (!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        //二级分类
        if (!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_id",subjectId);
        }
        //关注度
        if (!StringUtils.isEmpty(buyCountSort)){
            wrapper.orderByDesc("buy_count");
        }
        //价格
        if (!StringUtils.isEmpty(priceSort)){
            wrapper.orderByDesc("price");
        }
        //最新，创建时间
        if (!StringUtils.isEmpty(gmtCreateSort)){
            wrapper.orderByDesc("gmt_create");
        }
        baseMapper.selectPage(coursePage,wrapper);
        //总记录数
        long total = coursePage.getTotal();
        //数据集合
        List<EduCourse> courseList = coursePage.getRecords();
        //每页记录数
        long size = coursePage.getSize();
        //当前页
        long current = coursePage.getCurrent();
        //总页数
        long pages = coursePage.getPages();
        //是否有上一页
        boolean hasPrevious = coursePage.hasPrevious();
        //是否有下一页
        boolean hasNext = coursePage.hasNext();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("list",courseList);
        map.put("size",size);
        map.put("current",current);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return eduCourseMapper.getBaseCourseInfo(courseId);
    }
}
